<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>用户修改</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入" class="layui-input" id="username">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input type="text"  name="phone" lay-verify="title" autocomplete="off" placeholder="请输入" class="layui-input" id="phone">
                    </div>
                    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" onclick="getCode()" id="btn">
                        <i class="layui-icon layui-icon-ok"></i>
                        获取验证码
                    </button>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">验证码</label>
                    <div class="layui-input-block">
                        <input type="text"  name="code" lay-verify="title" autocomplete="off" placeholder="请输入" class="layui-input" id="code">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;

        //页面加载赋值
        $(function () {
            var username = parent.username;
            $("#username").val(username);
            var phone = parent.phone;
            $("#phone").val(phone);
        });

        form.on('submit(user-save)', function(data){
            let roleIds = "";
            $('input[type=checkbox]:checked').each(function() {
                roleIds += $(this).val()+",";
            });
            roleIds = roleIds.substr(0,roleIds.length-1);
            data.field.roleIds = roleIds;

            $.ajax({
                url:'/system/user/save',
                data:JSON.stringify(data.field),
                dataType:'json',
                contentType:'application/json',
                type:'post',
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });
    })
</script>
<script src="../../../component/jquery/jquery-2.1.4.js"></script>
<script>
    //获取验证码
    function getCode(){
        var phone = $("#phone").val();
        $.ajax({
            url:'/getCode',
            data: {"phone":phone},
            dataType:'json',
            contentType:'application/json',
            type:'get',
            success:function(result){
                if(result.code!=200){
                    layer.msg("获取失败",{icon:2,time:1000});
                }
            }
        })
    }
</script>
</body>
</html>
